package com.isczy.servicebase.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/********************************************
 * @Brief: springboot项目通用配置
 * @Version: 1.0
 * @Date: 2021年01月20日 10:12:12
 * @Author: ChangZiYang
 * ******************************************
 */
@Configuration
public class SpringBaseConfig {

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /***
     * ***************************************
     * 功能描述:配置视图资源解析，防止重定向解析失败
     * @param
     * @return {@link InternalResourceViewResolver}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 11:43】
     * @author modify:
     */
    @Bean
    public InternalResourceViewResolver viewResolver(){
        return new InternalResourceViewResolver();
    }

}
@Configuration
class WebMvcConfiguration extends WebMvcConfigurationSupport {

    /***
     * ***************************************
     * 功能描述:可配置自定义的资源处理
     * @param registry
     * @return
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 10:23】
     * @author modify:
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        super.addResourceHandlers(registry);
    }

    /***
     * ***************************************
     * 功能描述:全局跨域处理，无需再在每个controller上加@CrossOrigin
     * @param registry
     * @return
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 10:25】
     * @author modify:
     * 有了网关，此处不需要处理跨域了
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST","PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("access-control-allow-headers",
                        "access-control-allow-methods",
                        "access-control-allow-origin",
                        "access-control-max-age",
                        "X-Frame-Options")
                .allowCredentials(false).maxAge(3600);
        super.addCorsMappings(registry);
    }*/
}
